//
//  LRTopBar.m
//  Anyair
//
//  Created by karlcool on 14-5-12.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "LRTopBar.h"

@implementation LRTopBar

@synthesize leftBtn,rightBtn;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithTitle:(NSString*)titleStr AndLeft:(UIImage*)left AndRight:(UIImage*)right {
    
    if (IOS_7) {
        self = [super initWithFrame:CGRectMake(0, 0, 320, 64)];
    } else {
        self = [super initWithFrame:CGRectMake(0, 0, 320, 44)];
    }
    
    if (self) {
        center = titleStr;
        rightImage = right;
        leftImage = left;
    }
    
    return self;
}

- (id)initWithTitle:(NSString*)titleStr AndLeftText:(NSString*)left AndRightText:(NSString*)right {
    if (IOS_7) {
        self = [super initWithFrame:CGRectMake(0, 0, 320, 64)];
    } else {
        self = [super initWithFrame:CGRectMake(0, 0, 320, 44)];
    }
    
    if (self) {
        center = titleStr;
        rightStr = right;
        leftStr = left;
    }
    
    return self;
    
}

- (void)layoutSubviews {
    
    self.backgroundColor = AIR_GREEN;
    
    if (IOS_7) {
        self.frame = CGRectMake(0, 0, 320, 64);
    } else {
        self.frame = CGRectMake(0, 0, 320, 44);
    }
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, 320, 34)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = center;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLabel];
    
    leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(-5, 25, 60, 34)];
    if (leftImage!=nil) {
        [leftBtn setImage:leftImage forState:UIControlStateNormal];
    } else {
        [leftBtn setTitle:leftStr forState:UIControlStateNormal];
        [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [leftBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    }
    [leftBtn addTarget:self action:@selector(enterLeft:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftBtn];
    
    rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(320-5-60, 25, 60, 34)];
    if (rightImage!=nil) {
        [rightBtn setImage:rightImage forState:UIControlStateNormal];
    } else {
        [rightBtn setTitle:rightStr forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    }
    [rightBtn addTarget:self action:@selector(enterRight:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    
    if (!IOS_7) {
        titleLabel.frame = CGRectMake(0, 5, 320, 34);
        leftBtn.frame = CGRectMake(5, 5, 60, 34);
        rightBtn.frame = CGRectMake(320-5-60, 5, 60, 34);
    }
    
}


- (void)enterLeft:(UIButton*)sender {
    if ([self.delegete respondsToSelector:@selector(clickLeft:)]) {
        [self.delegete clickLeft:sender];
    }
}

- (void)enterRight:(UIButton*)sender {
    if ([self.delegete respondsToSelector:@selector(clickRight:)]) {
        [self.delegete clickRight:sender];
    }
}

@end
